package com.augmentum.login.dao.impl.mybatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.augmentum.login.Constants;
import com.augmentum.login.dao.BaseDao;
import com.augmentum.login.dao.ExamDao;
import com.augmentum.login.model.Exam;
import com.augmentum.login.model.Exam_question;
import com.augmentum.login.model.Page;
import com.augmentum.login.util.SqlMethod;
@Repository("examDao")
public class ExamDaoImpl extends BaseDao implements ExamDao {

    @Override
    public Exam getByName(String name) {
        return getSqlSession().selectOne(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_QUERY_BY_Name.getValue(), name);
    }

    @Override
    public void deleteBatchExams(String[] ids, int state) {
//        Map<String ,Object> map = new HashMap<String, Object>();
//        map.put(Constants.IDS, ids);
//        map.put(Constants.EXAM_STATE, state);
        
        if (state==0) {
            getSqlSession().delete(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.DRAFT_DELETE_BATCH.getValue(), ids);
        } else if (state==1) {
            getSqlSession().delete(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_DELETE_BATCH.getValue(), ids);
        }
       
    }

    @Override
    public int createExam(Exam exam) {
        return getSqlSession().insert(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_CREATE_METHOD.getValue(), exam);
    }

    @Override
    public void saveQuestionForBatch(List<Exam_question> list) {
        getSqlSession().insert(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.SAVE_QUESTION_BATCH.getValue(), list);
    }

    @Override
    public List<Exam> findExamsByPage(String name, String description, String order, Page page) {
        
        if (page.getCurrentPage() > page.getPageCount()) {
            page.setCurrentPage(page.getPageCount());
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Constants.EXAM_NAME, name);
        map.put(Constants.DESCRIPTION, description);
        map.put(Constants.ORDER, order);
        map.put(Constants.OFFSET, page.getOffSet());
        map.put(Constants.PAGE_SIEZ, page.getPageSize());
        return getSqlSession().selectList(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_QUERY_LIST_BY_PAGE.getValue(), map);
    }

    @Override
    public List<Exam> findDraftByPage(String name, Page page) {
        
        if (page.getCurrentPage() > page.getPageCount()) {
            page.setCurrentPage(page.getPageCount());
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Constants.EXAM_NAME, name);
        map.put(Constants.OFFSET, page.getOffSet());
        map.put(Constants.PAGE_SIEZ, page.getPageSize());
        return getSqlSession().selectList(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.DRAFT_QUERY_LIST_BY_PAGE.getValue(), map);
    }

    @Override
    public int getExamCount(String name, String description) {
        Map<String, String> map = new HashMap<String, String>();
        map.put(Constants.EXAM_NAME, name);
        map.put(Constants.DESCRIPTION, description);
        return getSqlSession().selectOne(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_GET_COUNT.getValue(), map);
    }
    
    @Override
    public int getDraftCount(String name) {
        return getSqlSession().selectOne(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.DRAFT_COUNT.getValue(), name);
    }

    @Override
    public Exam getById(int id) {
        return getSqlSession().selectOne(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.GET_BY_ID.getValue(), id);
    }

    @Override
    public void updateState(String[] ids) {
        getSqlSession().update(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.UPDATE_STATE.getValue(), ids);
    }

    @Override
    public void updateExam(Exam exam) {
        getSqlSession().update(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.EXAM_UPDATE.getValue(), exam);
    }
    
    @Override
    public void deleteQuestionForBatch(List<Exam_question> list) {
        getSqlSession().update(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.DELETE_QUESTION_STATE.getValue(), list);
    }
    
    @Override
    public List<Exam_question> getExamQuestion(int examId) {
        return getSqlSession().selectList(SqlMethod.EXAM_CLASS.getValue() + SqlMethod.GET_EXAM_QUESTION.getValue(), examId);
    }
}
